package com.example.code.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class CallableDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        FutureTask<Integer> futureTask = new FutureTask<>(new ThreadDemo());

        new Thread(futureTask).start();
//        new Thread(futureTask).start(); 这里调用第两次是不会进入的

        int a = 1;
        int b = futureTask.get(); // 获取callable线程的计算结果，如过没有计算完成会阻塞，直到计算完成之后返回

        System.out.println(b + a);
    }

}

class ThreadDemo implements Callable<Integer> {

    @Override
    public Integer call() throws Exception {
        System.out.println(Thread.currentThread().getName() + "进入call()*****");
        return 1024;
    }
}